package io.renren.modules.erp.controller;

import io.renren.common.utils.R;
import io.renren.modules.erp.service.impl.MsgServiceImpl;
import io.renren.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ji sheng hua jshERP
 */
@RestController
@RequestMapping(value = "/msg")
public class MsgController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(MsgController.class);

    @Resource
    private MsgServiceImpl msgService;

    @GetMapping("/getMsgByStatus")
    public R getMsgByStatus(@RequestParam("status") String status,
                            HttpServletRequest request)throws Exception {
            return R.ok().put("data",msgService.getMsgByStatus(status));
    }

    @PostMapping("/batchUpdateStatus")
    public R batchUpdateStatus(@RequestParam("ids") String ids,
                                              @RequestParam("status") String status,
                                              HttpServletRequest request)throws Exception {
            msgService.batchUpdateStatus(ids, status);
        return R.ok();
    }

    @GetMapping("/getMsgCountByStatus")
    public R getMsgCountByStatus(@RequestParam("status") String status,
                                                HttpServletRequest request)throws Exception {
            Map<String, Long> map = new HashMap<String, Long>();
            Long count = msgService.getMsgCountByStatus(status,getUserId());
            map.put("count", count);
            return R.ok().put("data",map);
    }


    @PostMapping(value = "/add")
    public R insert(@RequestBody String beanJson, HttpServletRequest request)throws Exception {
        if(1==msgService.insertMsg(beanJson, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody String beanJson,@RequestParam("id") Long id, HttpServletRequest request)throws Exception {
        if(1==msgService.updateMsg(beanJson, id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/delete")
    public R delete(@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==msgService.deleteMsg(id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/batchDelete")
    public R batchDelete(@RequestParam("ids") String ids, HttpServletRequest request)throws Exception {
        if(1==msgService.batchDeleteMsg(ids, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/checkIsNameExist")
    public R checkIsNameExist(@RequestParam("id") Long id, @RequestParam("name")String name)throws Exception {
        if(0<msgService.checkIsNameExist(id, name)){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
